package com.jzo2o.foundations.handler;


import com.jzo2o.api.foundations.dto.response.RegionSimpleResDTO;
import com.jzo2o.foundations.constants.RedisConstants;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.service.HomeService;
import com.jzo2o.foundations.service.IRegionService;
import com.jzo2o.foundations.service.IServeService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * xxl-job分布式任务调度，同步缓存数据
 * 任务类 - 任务方法由执行器去调用
 */
@Component
@Slf4j
public class SpringCacheSyncHandler {
    @Resource
    private IRegionService regionService; //区域服务
    @Resource
    private RedisTemplate<String, Object> redisTemplate; //redisTemplate
    @Resource
    private HomeService homeService; //首页服务
    @Resource
    private IServeService serveService; //服务服务


    /**
     * 用户端-已开通服务区域-缓存更新
     * 写完需去任务调度中心配置任务策略---配置为每日凌晨1点执行
     */
    @XxlJob(value = "activeRegionCacheSync") //指定任务名称
    public void activeRegionCacheSync() {

        //1、根据key清理原缓存
        //①---删除已开通服务区域列表
        redisTemplate.delete(RedisConstants.CacheName.JZ_CACHE + "::ACTIVE_REGIONS");
        //2、因为方法上已添加SpringCache注解，调用即可刷新缓存
        //通过查询开通区域列表进行缓存
        regionService.queryActiveRegionListCache();
    }

    /**
     * 用户端首页所选城市服务缓存更新
     * 每日凌晨1点执行
     * TODO 待测试
     */
    @XxlJob(value = "cityServeCacheSync")
    public void cityServeCacheSync() {
        List<RegionSimpleResDTO> regionSimpleResDTOS = regionService.queryActiveRegionListCache();
        //3、遍历区域对该区域下的服务类型进行缓存
        regionSimpleResDTOS.forEach(item -> {
            //区域id
            Long regionId = item.getId();
            //②---删除该区域下的首页服务列表
            String serve_type_key = RedisConstants.CacheName.SERVE_ICON + "::" + regionId;
            redisTemplate.delete(serve_type_key);
            //调用首页服务列表的查询方法去添加缓存
            homeService.queryServeIconCategoryByRegionIdCache(regionId);

            //③---删除该区域下的服务类型列表缓存
            String serve_icon_key = RedisConstants.CacheName.SERVE_TYPE + "::" + regionId;
            redisTemplate.delete(serve_icon_key);
            homeService.getServeTypeList(regionId);

            //④---删除该区域下的热门服务列表缓存
            String serve_hot_key = RedisConstants.CacheName.HOT_SERVE + "::" + regionId;
            redisTemplate.delete(serve_hot_key);
            homeService.queryHotServeListByRegionIdCache(regionId);

        });
    }

    /**
     * 热门服务详情缓存更新
     * 每3小时执行
     */
    @XxlJob(value = "hotServeCacheSync")
    public void hotServeCacheSync() {
        log.info(">>>>>>>>开始进行缓存同步，更新热门服务详情");

        //1.查询热门且上架状态的服务
        List<Serve> hotAndOnSaleServeList = serveService.queryHotAndOnSaleServeList();
        Set<Long> hotServeItemIds = new HashSet<>();

        //2.热门服务缓存续期
        for (Serve serve : hotAndOnSaleServeList) {
            //2.1删除热门服务缓存
            String serveKey = RedisConstants.CacheName.SERVE + "::" + serve.getId();
            redisTemplate.delete(serveKey);

            //2.2重置热门服务缓存
            homeService.queryServeByIdCache(serve.getId());

            //2.2提取热门服务对应的服务项id
            hotServeItemIds.add(serve.getServeItemId());
        }

        //3.对热门服务项更新缓存
        for (Long serveItemId : hotServeItemIds) {
            //3.1删除热门服务项缓存
            String serveKey = RedisConstants.CacheName.SERVE_ITEM + "::" + serveItemId;
            redisTemplate.delete(serveKey);

            //3.2重置热门服务项缓存
            homeService.queryServeItemByIdCache(serveItemId);
        }
        log.info(">>>>>>>>更新热门服务详情完成");
    }
}
